<template>
  <div class="page-filter">
    <div class="page-filter-left">
      <a-button type="primary" @click="addRole">添加角色</a-button>
      <a-button @click="batchStatus(1)">启用</a-button>
      <a-button @click="batchStatus(2)">禁用</a-button>
      <a-button danger @click="deleteFilmMaker">删除</a-button>
    </div>
    <div class="page-filter-right">
      <div class="show">
        <a-select
          v-model:value="roleState.role_status"
          placeholder="全部状态"
          class="select-m-r-24"
          :getPopupContainer="(triggerNode: any) => triggerNode.parentNode"
        >
          <a-select-option :value="undefined">全部状态</a-select-option>
          <a-select-option :value="1">启用</a-select-option>
          <a-select-option :value="2">禁用</a-select-option>
        </a-select>
        <a-input
          :maxlength="19"
          :onkeyup="
            () => {
              if (roleState.role_id) roleState.role_id = roleState.role_id.replace(/\D/g, '')
            }
          "
          :onafterpaste="
            () => {
              if (roleState.role_id) roleState.role_id = roleState.role_id.replace(/\D/g, '')
            }
          "
          v-model:value.trim="roleState.role_id"
          class="input-m-r-16 w-[250px]"
          placeholder="请输入角色ID"
        />
        <a-input
          v-model:value.trim="roleState.role_keyword"
          class="input-m-r-16 w-[250px]"
          placeholder="请输入角色名称关键词"
        />
        <a-input
          v-model:value.trim="roleState.person_keyword"
          class="input-m-r-16 w-[250px]"
          placeholder="请输入影人名称关键词"
        />
        <a-button type="primary" class="m-r-8" @click="handleSearch">搜索</a-button>
        <a-button class="btn-m-r-24" @click="handleReset">重置</a-button>
      </div>
    </div>
  </div>
  <!-- 删除对话框 -->
  <modal-view
    title="提示"
    v-model:visible="deleteVisible"
    okText="确定"
    cancelText="取消"
    :width="458"
    @confirm="deleteConfirm"
    @cancel="deleteCancel"
  >
    <span>确定要删除该角色吗</span>
  </modal-view>
</template>
<script lang="ts" setup>
import { roleState, resetState, statusState } from '../state'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import ModalView from '@/components/components/modal/ModalView.vue'
import apis from '@/apis'
import { onMounted, ref } from 'vue'
const {
  site: { role_del, edit_status }
} = apis
const deleteVisible = ref(false)
const router = useRouter()
const emit = defineEmits(['update-list'])
const deleteFilmMaker = () => {
  if (!statusState.selectedRowKeys.length) return message.warning('请选择至少一个角色进行删除')
  deleteVisible.value = true
}
//新建分类
const addRole = () => {
  router.push({ name: 'EditRole' })
}
const handleSearch = () => {
  console.log(roleState)
  emit('update-list')
}
const handleReset = () => {
  resetState()
  console.log(roleState)
  emit('update-list')
}
/**
  删除分类对话框事件
 */
const deleteConfirm = (value: any) => {
  console.log('value: ', value)
  role_del({ role_id: statusState.selectedRowKeys }).then(() => {
    message.success('批量删除成功')
    deleteVisible.value = value
    emit('update-list')
  })
}
const deleteCancel = () => {
  deleteVisible.value = false
  statusState.selectedRowKeys = []
}
//批量编辑状态
const batchStatus = (status: number) => {
  if (!statusState.selectedRowKeys.length) return message.warning('请选择至少一个角色进行编辑')
  const data = {
    type: 11,
    status: status === 1 ? 1 : 2,
    id: statusState.selectedRowKeys
  }
  edit_status(data).then(() => {
    message.success(status === 1 ? '启用成功' : '禁用成功')
    statusState.selectedRowKeys = []
    emit('update-list')
  })
}
onMounted(() => {
  statusState.selectedRowKeys = []
  resetState()
})
</script>

<style lang="less" scoped>
@common-color: #0083ff; // sea blue

.page-filter {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;

  &-left {
    display: flex;
    flex-wrap: nowrap;

    margin-top: 10px;

    .ant-btn:not(:last-child) {
      margin-right: 16px;
    }

    .ant-btn:last-child {
      background-color: rgba(254, 76, 77, 0.08);
      margin-right: 16px;
    }
  }

  &-right {
    display: flex;
    align-items: center;

    .select-m-r-24 {
      width: 196px;
      height: 32px;
      margin: 10px 24px 0 0;
    }

    .input-m-r-16 {
      height: 32px;
      margin: 10px 16px 0 0;
    }

    .btn-m-r-24 {
      margin: 10px 24px 0 0;
    }

    .m-r-8 {
      margin: 10px 8px 0 0;
    }
  }
}
</style>
